package com.xy.novelmanagement.controller;

import com.xy.model.RestfulResult;
import com.xy.model.RootNovelType;
import com.xy.novelmanagement.service.INovelClassificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("novelclassification")
@Slf4j
public class NovelClassificationController {
		@Autowired
		private INovelClassificationService novelClassificationService;

		@GetMapping(value = "/")
		public RestfulResult getList() {
				List<RootNovelType> list = novelClassificationService.getTypeList();
				log.info("NovelClassificationController getList [list = ]" + list);
				return RestfulResult.success(list);
		}

		@PutMapping(value = "/{typeName}", produces = "application/json;charset=UTF-8")
		public RestfulResult updateType(@PathVariable String typeName, @RequestBody RootNovelType rootNovelType) {
				log.info("NovelClassificationController updateType [typeName = ]" + typeName);
				log.info("NovelClassificationController updateType [rootNovelType = ]" + rootNovelType);
				if (typeName == null || typeName.equals("")) {
						return RestfulResult.failed("-403", "类型名不能为空！");
				}
				int resultCode = novelClassificationService.updateType(typeName, rootNovelType);
				if (resultCode == -1) {
						return RestfulResult.failed("-400", "类型不存在或存在多个！");
				} else if (resultCode == 0) {
						return RestfulResult.failed("-400", "更新失败！");
				}
				return RestfulResult.success();
		}

		@PostMapping(value = "/")
		public RestfulResult addType(@ModelAttribute RootNovelType novelType) {
				log.info("NovelClassificationController  addType [RootNovelType = ]" + novelType);
				if (novelType.getTypeName() == null || novelType.getTypeName().equals("")) {
						return RestfulResult.failed("-403", "类型名不能为空，添加失败！");
				}
				int resultCode = novelClassificationService.addType(novelType);
				if (resultCode == 200) {
						return RestfulResult.failed("-500", "插入的类型以存在！");
				} else if (resultCode == -200) {
						return RestfulResult.failed("-500", "插入的类型以存在！");
				} else if (resultCode == -1) {
						return RestfulResult.failed("-404", "服务器发生了错误！");
				}

				return RestfulResult.success();
		}

		@DeleteMapping(value = "/{ids}")
		public RestfulResult deleteType(@PathVariable String ids) {
				log.info("NovelClassificationController deleteType [ids = ]" + ids);
				if (ids == null || ids.equals("")) {
						return RestfulResult.failed("-403", "要删除的类型名不能为空！");
				}else if (ids.equals(",")){
						return RestfulResult.failed("-403", "要删除的类型名不能为[ , ]！");
				}
				ids = ids.replace("玄幻", "").
					replace("历史", "").
					replace("科幻", "").
					replace("都市", "").
					replace("军事", "").
					replace("武侠", "").
					replace("网游", "").
					replace("仙侠", "").
					replace("体育", "").
					replace("悬疑", "");
				log.info("NovelClassificationController deleteType [ids = ]" + ids);
				log.info("NovelClassificationController deleteType [ids!! = ]" + Arrays.toString(ids.split(","))+ids.split(",").length);
				if (ids.equals("")||ids.split(",").length==0){
						return RestfulResult.failed("-403", "要删除的类型名不能为历史,科幻,都市,军事,武侠,网游,仙侠,体育,悬疑等基础类型！");
				}
				String[] idList;
				if (ids.contains(",")) {
						idList = ids.split(",");
				} else {
						idList = new String[]{ids};
				}
				log.info("NovelClassificationController deleteType [idList = ]" + idList);
				boolean result = novelClassificationService.deleteType(idList);
				return RestfulResult.success(result);
		}
}
